<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
include 'header.php';
include 'menu.php';
?>

</head>

<body>
<br><br>
<?php 

// Function to load xml file, get variables from xml data and 
// create a multi dimensional array to pass back to the caller

function getSettings()
{
	$configfile='/etc/opt/fbc/fbc.xml';
	$xml = simplexml_load_file($configfile);
    $backuppath = $xml->backuppath;
    $mountpath = $xml->mountpath;
    $tarpath = $xml->tarpath;
    $temppath = $xml->temppath;
    $smtphost = $xml->smtphost;
    $emailaddr = $xml->emailaddr;
    $maxinc = $xml->maxinc;
    $startday = $xml->startday;
    $offlinepath = $xml->offlinepath;
    $loglevel = $xml->loglevel;
    $logpath = $xml->logpath;
	$sendlog = $xml->sendlog;
	$gwtimestamp = $xml->gwtimestamp;

	// this sets up the array as key/value pairs.
	// for example the first entry, key is 'backuppath' and value is '/var/fbc/backup'
	// assuming that's what's in the xml file as the value.
	// to access the value from a php point of view, it is:
	//  $settings[backuppath] returns /var/fbc/backup

	$settings = array(
		"backuppath"=>$backuppath, 
		"mountpath"=>$mountpath,
		"tarpath"=>$tarpath,
		"temppath"=>$temppath,
		"offlinepath"=>$offlinepath,
		"logpath"=>$logpath,
		"loglevel"=>$loglevel,
		"smtphost"=>$smtphost,
		"emailaddr"=>$emailaddr,
		"maxinc"=>$maxinc,
		"startday"=>$startday,
		"sendlog"=>$sendlog,
		"gwtimestamp"=>$gwtimestamp
		);
		    
    return $settings;
}

// check if teh submit button was hit,  if so, get the POST data
// then modify the xml data with the new data.  see below.

if (isset($_POST['submit']))
{
	$configfile='/etc/opt/fbc/fbc.xml';
	$xml = simplexml_load_file($configfile);
	$backuppath=$_POST['backuppath'];
	$mountpath=$_POST['mountpath'];
	$offlinepath=$_POST['offlinepath'];
	$tarpath=$_POST['tarpath'];
	$temppath=$_POST['temppath'];
	$logpath=$_POST['logpath'];
	$loglevel=$_POST['loglevel'];
	$emailaddr=$_POST['emailaddr'];
	$smtphost=$_POST['smtphost'];
	$startday=$_POST['startday'];
	$maxinc=$_POST['maxinc'];
	$sendlog=$_POST['sendlog'];
	$gwtimestamp=$_POST['gwtimestamp'];

	// Here's where we replace the xml values with changes posted via the form.	

	$xml->backuppath = $backuppath;
	$xml->mountpath = $mountpath;
	$xml->offlinepath = $offlinepath;
	$xml->tarpath = $tarpath;
	$xml->temppath = $temppath;
	$xml->logpath = $logpath;
	$xml->loglevel = $loglevel;
	$xml->smtphost = $smtphost;
	$xml->emailaddr = $emailaddr;
	$xml->maxinc = $maxinc;
	$xml->startday = $startday;
	$xml->sendlog = $sendlog;
	$xml->gwtimestamp = $gwtimestamp;

	// Write the file back to disk.

	file_put_contents($configfile, $xml->asXML()) or die("ERROR:  Could not save config file");

	// now reload the array from the getSettings function.
	$values = getSettings();

}
?>

<?php

//  Call function to get the xml data
//  so to get the xml data,  it would be $values['backuppath']

$values = getSettings();

?>

<!--  create the form in a table -->
<br>
<b>Main Settings</b>
<hr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"/>
<table>
<br>
<tr><td><b>Backup Path: </b></td>
<td width="200"><input name="backuppath" type="text" value="<?php echo $values[backuppath];?>" ></input></td>
<td><b>Mount Path: </b></td>
<td><input name="mountpath" type="text" value="<?php echo $values[mountpath];?>" ></input></td></tr>
<tr><td><b>Restore Path: </b></td>
<td><input name="offlinepath" type="text" value="<?php echo $values[offlinepath];?>"></input></td>
<td><b>Tar Path: </b></td>
<td><input name="tarpath" type="text" value="<?php echo $values[tarpath];?>"></input></td></tr>
<tr><td><b>Temp Path: </b></td>
<td><input name="temppath" type="text" value="<?php echo $values[temppath];?>"></input></td>
<td><b>Log Path: </b></td>
<td><input name="logpath" type="text" value="<?php echo $values[logpath];?>"></input></td></tr>
<tr><td>&nbsp</td></tr>
<tr><td><b>Log Level: </b></td>
<td>Normal <input type="radio" value="Normal" name="loglevel"
<?php if ($values[loglevel] == "Normal") echo "checked";?> >
Verbose <input type="radio" value="Verbose" name="loglevel"
<?php if ($values[loglevel] == "Verbose") echo "checked";?> ></td>
<td><b>Set GroupWise Time Stamp?: &nbsp&nbsp</b></td>
<td>Yes<input type="radio" value="Yes" name="gwtimestamp"
<?php if ($values[gwtimestamp] == "Yes") echo "checked";?> >
No<input type="radio" value="No" name="gwtimestamp"
<?php if ($values[gwtimestamp] == "No") echo "checked";?> ></td>
</tr>
<tr><td><b>Max Incremental Backups: </b></td>
<td><select name="maxinc">
      <option value="<?php echo $values[maxinc]; ?>" selected="selected"><?php echo $values[maxinc]; ?></option>
      <option value="<?php echo $values[maxinc]; ?>"> -- </option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
	  </select></td>
<td><b>Start Day: </b></td>
<td><select name="startday">
      <option value="<?php echo $values[startday]; ?>" selected="selected"><?php echo $values[startday]; ?></option>
      <option value="<?php echo $values[startday]; ?>"> -- </option>
      <option value="Sunday">Sunday</option>
      <option value="Monday">Monday</option>
      <option value="Tuesday">Tuesday</option>
      <option value="Wednesday">Wednesday</option>
      <option value="Thursday">Thursday</option>
      <option value="Friday">Friday</option>
      <option value="Saturday">Saturday</option>
    </select></td></tr>
<tr><td>&nbsp</td></tr>
<tr><td><b>Send Log File: </b></td>
<td>Yes<input type="radio" value="Yes" name="sendlog"
<?php if ($values[sendlog] == 'Yes') echo 'checked';?> >
	No<input type="radio" value="No" name="sendlog"  
<?php if ($values[sendlog] == 'No') echo 'checked';?> ></td></tr>
<tr><td><b>SMTP Host: </b></td>
<td><input name="smtphost" type="text" value="<?php echo $values[smtphost];?>"></input></td>
<td><b>Email Address: </b></td>
<td><input name="emailaddr" type="text" value="<?php echo $values[emailaddr];?>"></input></td></tr>
<tr><td>&nbsp</td></tr>
<tr><td><input type="submit" name="submit" value="Save Changes"></input>
</form></td>
<td><button onClick="window.open('help.html')">Help</button></td></tr>
</table>
<br><br>

</body>
</html>
<?php include 'footer.php'; ?>
